package com.medication.config;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.SqlServerDialect;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;
import com.medication.controller.AdminRoute;
import com.medication.model.LimitMedication;
import com.medication.model.ThreeMedicationDir;
import com.medication.model._MappingKit;

public class ServerConfig extends JFinalConfig {

	@Override
	public void configConstant(Constants me) {
		me.setDevMode(PropKit.getBoolean("devMode", true));
		me.setEncoding("utf-8");
		me.setViewType(ViewType.FREE_MARKER);

	}

	@Override
	public void configRoute(Routes me) {
		me.add(new AdminRoute());
	}

	@Override
	public void configPlugin(Plugins me) {
		C3p0Plugin c3p0Plugin = new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("user"),
				PropKit.get("password").trim(),PropKit.get("driver").trim());
		me.add(c3p0Plugin);

		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		arp.setDialect(new SqlServerDialect());
		arp.setShowSql(true);
		arp.addMapping("limit_medication", "id",LimitMedication.class);
		arp.addMapping("three_medication_dir","id", ThreeMedicationDir.class);
		_MappingKit.mapping(arp);
		me.add(arp);

		EhCachePlugin ecp = new EhCachePlugin();
		me.add(ecp);
	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.addGlobalActionInterceptor(new GlobalActionInterceptor());
	}

	@Override
	public void configHandler(Handlers me) {
		me.add(new ContextPathHandler("ctx"));
		me.add(new RequestHandler());
	}

	@Override
	public void afterJFinalStart() {
		// 1.5 之后支持redis存储access_token、js_ticket，需要先启动RedisPlugin
		// ApiConfigKit.setAccessTokenCache(new RedisAccessTokenCache());
		// 1.6新增的2种初始化
		// ApiConfigKit.setAccessTokenCache(new
		// RedisAccessTokenCache(Redis.use("weixin")));
		// ApiConfigKit.setAccessTokenCache(new
		// RedisAccessTokenCache("weixin"));
	}

	public static void main(String[] args) {
		JFinal.start("webapp", 8090, "/", 5);
	}

	@Override
	public void configEngine(Engine me) {
		// TODO Auto-generated method stub

	}

}
